//路由器
app.config(function($routeProvider){
    $routeProvider.
    when("/",{
       controller: 'ind',
       templateUrl: 'view/test.html'
    }).
    when("/index",{
       //首页，默认为手表信息管理页
       controller: 'ind',
       templateUrl: 'view/test.html'
    }).
    when("/use_aly",{
       //使用频率分析
       controller: 'usAly',
       templateUrl: 'view/use_aly.html'
    }).
    when("/map_aly",{
       //用户地域分析页面
       controller: 'map',
       templateUrl: 'view/map_aly.html',
       resolve: {
          async: getJSON(urlAddress + "/home/analysis/json/map.json")
       }
    }).
    when("/hot_aly",{
       // 用户活跃分析页面
       controller: 'hot_aly',
       templateUrl: 'view/hot_aly.html',
       resolve : {
          async : getJSON(urlAddress + "/home/analysis/json/hot_aly.json")
       }
    }).
    otherwise({
       redirectTo:'/'
    });
});

//用户使用频率分析控制器
app.controller('usAly', function($scope){
    $scope.dataShow = true;

    $(".tap_md div").on("click",function(){
        if ($(this).hasClass("un_on")) {
          $(this).removeClass("un_on").siblings().addClass("un_on");
           var str = $(this).html();
           $scope.drawFn.drawColumn($('.form_col'),{
              title : str,
              seriseArr : colArr
           });
        };
    })

    //需要的数据原型
    var colData = [
        {
            name: '亮屏',
            y : 800,
            data : [7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '表情',
            y : 810,
            data : [17, 18, 19, 11, 18, 25, 28, 15, 13, 13, 19, 16,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '拨打电话',
            y : 700,
            data : [27, 18, 19, 21, 28, 15, 18, 27, 23, 13, 19, 26,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '接听电话',
            y : 660,
             data : [23, 12, 11, 26, 28, 12, 18, 27, 23, 17, 12, 24,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '拒听电话',
            y : 350,
            data : [23, 12, 11, 26, 28, 12, 18, 27, 23, 17, 12, 24,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '发送语音',
            y : 450,
            data : [7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '接收语音',
            y : 550,
            data : [7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '礼物',
            y : 650,
            data : [27, 18, 19, 21, 28, 15, 18, 27, 23, 13, 19, 26,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '加好友',
            y : 732,
            data : [27, 18, 19, 21, 28, 15, 18, 27, 23, 13, 19, 26,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '二维码',
            y : 654,
            data : [27, 18, 19, 21, 28, 15, 18, 27, 23, 13, 19, 26,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: 'SOS',
            y : 333,
            data : [27, 18, 19, 21, 28, 15, 18, 27, 23, 13, 19, 26,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: 'home键',
            y : 244,
            data : [23, 12, 11, 26, 28, 12, 18, 27, 23, 17, 12, 24,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        },{
            name: '重启',
            y : 544,
            data : [23, 12, 11, 26, 28, 12, 18, 27, 23, 17, 12, 24,7, 8, 9, 11, 18, 25, 28, 15, 13, 13, 19, 16]
        }
    ];

    // 传递给第一个柱状图的数组
    var colArr = [];
    for (var i = 0; i < colData.length; i++) {
        colArr[i] = [colData[i].name,colData[i].y,colData[i].data];
    };

    var lineArr = [];
    for (var i = 0; i < 1; i++) {
        lineArr[i] = {
            name : colData[i].name,
            data : colData[i].data
        }
    };
    var xName = ['1', '2', '3', '4', '5', '6','7', '8', '9', '10', '11', '12','13', '14', '15', '16','17', '18', '19', '20','21', '22', '23', '24'];

    function clickDrawLine(str,arr) {
        var lines = [];
        lines[0] = {
            name : str,
            data : arr
        }
        str = str + "使用统计表";
        $scope.drawFn.drawLine($('.form_line'),{
            title : str,
            seriseArr : lines,
            xcate : xName,
            colorArr : ["#4aa7ea"]
        });
    }

    // 获得绘图所需要的方法
    $scope.drawFn = drawFn();
    $scope.drawFn.drawColumn($('.form_col'),{
        title : "周使用统计表",
        seriseArr : colArr,
    },clickDrawLine);
    $scope.drawFn.drawLine($('.form_line'),{
          title : "亮屏使用统计表",
          seriseArr : lineArr,
          xcate : xName,
          colorArr : ["#4aa7ea"]
    });

    // 日期相关模块
    $scope.getNewData = function(da) {
        console.log(da.year + "年" + da.month + "月");
        $(".now_day").html(da.year + "年" + da.month + "月" + da.date + "日");
        $scope.data = da;
    }

    //创建日期组件
    $scope.dataFn = new dataFn();
    $scope.dataFn.creat($("#data"),$scope.getNewData);

    $scope.prvDay = function() {
    $scope.data.date -= 1;
    if ($scope.data.date <= 0) {
      $scope.data.month -= 1
      if(month <= 0) {
         $scope.data.month = 12;
         $scope.data.year -= 1
      }
      $scope.data.date = $scope.dataFn.getMaxDay($scope.data.month,$scope.data.year);
    }
    $scope.dataFn.init($("#data"),$scope.data);
    $scope.getNewData($scope.data);
    }

    $scope.nowDay = function() {
        if ($scope.dataShow) {
          $scope.dataShow = false;
        } else {
          $scope.dataShow = true;
        }
    }
    $scope.nextDay = function() {
        var maxDays = $scope.dataFn.getMaxDay($scope.data.month,$scope.data.year);
        $scope.data.date += 1;
        if ($scope.data.date > maxDays) {
            $scope.data.date = 1;
            $scope.data.month += 1
            if(month > 12) {
               $scope.data.month = 1;
               $scope.data.year += 1
            }
        }
        $scope.dataFn.init($("#data"),$scope.data);
        $scope.getNewData($scope.data);
    }
    navTitClass($("nav .use_aly").parent().eq(0));
})

 // 用户活跃分析页的控制器
app.controller('hot_aly',["$scope","async",function($scope,msg){
    //初始化数值
    $scope.allDevice = msg.init.allDevice;
    $scope.yestadyAdd = msg.init.yestadyAdd;
    $scope.yestadyOlyDev = msg.init.yestadyOlyDev;
    $scope.yestadyOly = msg.init.yestadyOly;
    $scope.seven = msg.init.seven;
    $scope.thirty = msg.init.thirty;
    $scope.sos = msg.init.sos;

    //引入获取的数据及绘画封装函数并绘制图像
    $scope.select1 = msg.datas;
    $scope.select2 = msg.datas2;
    $scope.drawFn = drawFn();
    $scope.draw1 = function(data) {
        $scope.selectTit1 = data.name;
         var arr = [];
        for (var i = 1; i <= data.data.length; i++) {
            arr[i] =  i;
        };
        $scope.drawFn.drawColumn($('.day_file'),{
           seriseArr : data.data,
           xcate : arr,
           format : data.name.replace("每月","")  + ': <b>{point.y:.0f}</b>'
        });
        $scope.selectShow1 = false;
    }
    $scope.draw2 = function(data) {
        $scope.selectTit2 = data.name;
        $scope.drawFn.drawColumn($('.month_file'),{
           seriseArr : data.data,
           format : data.name.replace("每日","") + ': <b>{point.y:.0f}</b>'
        });
        $scope.selectShow2 = false;
    }
    $scope.draw1($scope.select1[0]);
    $scope.draw2($scope.select2[0]);

    $scope.left = function($event) {
        var _this = $event.target;
        var t = $(_this).next().width();
        $(_this).next().stop().animate({
            "scrollLeft" : $(_this).next().scrollLeft() - t/2
        },800);
    }
    $scope.right = function($event) {
        var _this = $event.target;
        var t = $(_this).prev().width();
        $(_this).siblings(".drawf").stop().animate({
            "scrollLeft" : $(_this).prev().scrollLeft() + t/2
        },800);
    }

    navTitClass($("nav .hot_aly").parent().eq(0));
}])

// 用户活跃分析页的控制器
app.controller('hot_aly',["$scope","async",function($scope,msg){
      //初始化数值
      $scope.allDevice = msg.init.allDevice;
      $scope.yestadyAdd = msg.init.yestadyAdd;
      $scope.yestadyOlyDev = msg.init.yestadyOlyDev;
      $scope.yestadyOly = msg.init.yestadyOly;
      $scope.seven = msg.init.seven;
      $scope.thirty = msg.init.thirty;
      $scope.sos = msg.init.sos;

      //引入获取的数据及绘画封装函数并绘制图像
      $scope.select1 = msg.datas;
      $scope.select2 = msg.datas2;
      $scope.drawFn = drawFn();
      $scope.draw1 = function(data) {
          $scope.selectTit1 = data.name;
           var arr = [];
          for (var i = 1; i <= data.data.length; i++) {
              arr[i] =  i;
          };
          $scope.drawFn.drawColumn($('.day_file'),{
             seriseArr : data.data,
             xcate : arr,
             format : data.name.replace("每月","")  + ': <b>{point.y:.0f}</b>'
          });
          $scope.selectShow1 = false;
      }
      $scope.draw2 = function(data) {
          $scope.selectTit2 = data.name;
          $scope.drawFn.drawColumn($('.month_file'),{
             seriseArr : data.data,
             format : data.name.replace("每日","") + ': <b>{point.y:.0f}</b>'
          });
          $scope.selectShow2 = false;
      }
      $scope.draw1($scope.select1[0]);
      $scope.draw2($scope.select2[0]);

      $scope.left = function($event) {
          var _this = $event.target;
          var t = $(_this).next().width();
          $(_this).next().stop().animate({
              "scrollLeft" : $(_this).next().scrollLeft() - t/2
          },800);
      }
      $scope.right = function($event) {
          var _this = $event.target;
          var t = $(_this).prev().width();
          $(_this).siblings(".drawf").stop().animate({
              "scrollLeft" : $(_this).prev().scrollLeft() + t/2
          },800);
      }

      navTitClass($("nav .hot_aly").parent().eq(0));
}])

//用户使用地域分析控制器
app.controller('map',["$scope","async",function($scope,async) {
      navTitClass($("nav .map_aly").parent().eq(0));

      // 初始化数据
      $scope.opints = async.data;
      $scope.minValue = $scope.opints[1].value;
      $scope.maxValue = $scope.opints[1].value;
      $scope.sum = 0;
      for (var i = 0; i < $scope.opints.length; i++) {
          if ($scope.opints[i].value < $scope.minValue) {
            $scope.minValue = $scope.opints[i].value
          }
      };
      for (var i = 0; i < $scope.opints.length; i++) {
        if ($scope.opints[i].value > $scope.maxValue) {
          $scope.maxValue = $scope.opints[i].value
        }
      };
      for (var i = 0; i < $scope.opints.length; i++) {
          $scope.sum += $scope.opints[i].value
      };
      function setOtherEle() {
          var w = $scope.minValue + $scope.maxValue
          for (var i = 0; i < $scope.opints.length; i++) {
              $scope.opints[i].num = ($scope.opints[i].value*100 / w).toFixed(2);
              $scope.opints[i].pe = ($scope.opints[i].value*100 / $scope.sum).toFixed(2);
          };
      }
      setOtherEle();

      // 滚动设置
      $scope.scroll = function(cityKey) {
        var tgCity;
        for (var i = 0; i < $scope.opints.length; i++) {
          if ($scope.opints[i].key == cityKey) {
            tgCity = $scope.opints[i].name;
            break;
          };
        };
        
          $(".cityName").each(function(){
              var str = $(this).html().split(" ");
              if(tgCity === str[1]) {
                  var n = parseInt(str[0]);
                  var a = (n - 1) * $(this).parent().outerHeight();
                  $(".citys").stop().animate({
                    "scrollTop" : a 
                  },500);
                  $(this).parent().addClass("checked").siblings().removeClass("checked");
              }
          }) 
      }

      $(window).on("beforeunload", function() {
            sessionStorage.removeItem('jsLoad');
      })
      var jsLoad = sessionStorage.getItem('jsLoad');
      if(!jsLoad) {
        $.getScript("http://code.highcharts.com/maps/modules/map.js",function(){
          $.getScript("http://code.highcharts.com/maps/modules/data.js",function(){
            $.getScript("http://code.highcharts.com/maps/modules/drilldown.js",function(){
              $.getScript("http://sandbox.runjs.cn/uploads/rs/228/zroo4bdf/cn-china-by-peng8.js",function(){
                  sessionStorage.setItem('jsLoad',"ok");
                  mapinit($scope.opints,$scope.scroll);
              });
            });
          });
        });
      } else {
        mapinit($scope.opints,$scope.scroll);
      }

}]);

// 测试页控制器
app.controller('ind', function($scope){
    $scope.sumDevice = 3227;
    $scope.bindDevice = 1277;
    $scope.calls = 277;
    $scope.yesterday = 1030;
 
    // 数据原型
     var barArr = [{
             name: '在线',
             y: 56.33
           }, {
               name: '离线',
               y: 24.03,
               sliced: true,
               selected: true
           }, {
               name: '停机',
               y: 12.2
          }]
     var colArr = [
          ['2016/12', 600],
          ['2016/12', 800],
          ['2016/12', 1000],
          ['2016/9', 1100],
          ['2016/9', 200],
          ['2016/9', 200],
          ['2016/9', 200],
          ['2016/9', 200],
          ['2016/9', 200],
          ['2016/9', 200],
          ['2016/9', 200],
          ['2016/9', 200]
       ]
    // 饼状图点击的回调函数
    function BarClickBack(json,es) {
       console.log(json);
       console.log(es);
    }

    // 获得绘图所需要的方法
   $scope.drawFn = drawFn();
   $scope.drawFn.drawBar($('#device_form'),'设备在线/离线/停机统计',barArr,BarClickBack);
   $scope.drawFn.drawColumn($('#new_Device'),{
        title : "最近一年新增设备",
        seriseArr : colArr
   });
   $scope.drawFn.drawColumn($('#new_User'),{
        title : "最近一年新增设备",
        seriseArr : colArr
   });


   indexOnload();
})

